using UnityEngine;

public class InputTracker
{
	private int trackingFingerId;

	public Rect viewportArea = new Rect(0f, 0f, 1f, 1f);

	public bool trackingTouch { get; protected set; }

	public bool isMouseDown
	{
		get
		{
			GetIsButtonDown();
			GetIsButtonUp();
			return trackingTouch;
		}
	}

	public Vector3 viewportPosition
	{
		get
		{
			GetIsButtonDown();
			GetIsButtonUp();
			Vector3 position = MousePosition();
			Vector3 result = Camera.main.ScreenToViewportPoint(position);
			result.x -= viewportArea.xMin;
			result.y -= viewportArea.yMin;
			result.x /= viewportArea.width;
			result.y /= viewportArea.height;
			return result;
		}
	}

	public Vector3 firstTouchPosition { get; protected set; }

	public Vector3 offsetFromFirstTouch
	{
		get
		{
			return MousePosition() - firstTouchPosition;
		}
	}

	public InputTracker(Rect viewportArea)
	{
		this.viewportArea = viewportArea;
	}

	public bool GetTrackedTouch(out Touch touch)
	{
		touch = default(Touch);
		if (Input.touchCount == 0)
		{
			return false;
		}
		for (int i = 0; i < Input.touchCount; i++)
		{
			Touch touch2 = Input.GetTouch(i);
			if (touch2.fingerId == trackingFingerId)
			{
				touch = touch2;
				return true;
			}
		}
		return false;
	}

	public bool isInsideViewportArea(Vector3 viewPortPosition)
	{
		return viewPortPosition.y <= viewportArea.yMax && viewPortPosition.y >= viewportArea.yMin && viewPortPosition.x <= viewportArea.xMax && viewPortPosition.x >= viewportArea.xMin;
	}

	public bool GetIsButtonDown()
	{
		if (trackingTouch)
		{
			return false;
		}
		trackingFingerId = -1;
		Camera main = Camera.main;
		RaycastHit hit;
		if (Input.touchCount != 0)
		{
			for (int i = 0; i < Input.touchCount; i++)
			{
				Touch touch = Input.GetTouch(i);
				if (touch.phase == TouchPhase.Began)
				{
					Vector3 viewPortPosition = main.ScreenToViewportPoint(touch.position);
					if (isInsideViewportArea(viewPortPosition) && (!UICamera.Raycast(touch.position, out hit) || !(hit.collider.GetComponent<InputControl>() == null)))
					{
						trackingFingerId = touch.fingerId;
						trackingTouch = true;
					}
				}
			}
		}
		else if (Input.GetMouseButtonDown(0))
		{
			Vector3 viewPortPosition2 = main.ScreenToViewportPoint(Input.mousePosition);
			if (!isInsideViewportArea(viewPortPosition2))
			{
				return false;
			}
			if (UICamera.Raycast(Input.mousePosition, out hit) && hit.collider.GetComponent<InputControl>() == null)
			{
				return false;
			}
			trackingTouch = true;
		}
		if (trackingTouch)
		{
			firstTouchPosition = MousePosition();
		}
		return trackingTouch;
	}

	public bool GetIsButtonUp()
	{
		if (!trackingTouch)
		{
			return false;
		}
		bool flag = false;
		if (Input.touchCount != 0)
		{
			Touch touch;
			if (!GetTrackedTouch(out touch))
			{
				return false;
			}
			flag = touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled;
		}
		else
		{
			flag = Input.GetMouseButtonUp(0);
		}
		if (flag)
		{
			trackingTouch = false;
		}
		return flag;
	}

	public Vector3 MousePosition()
	{
		if (Input.touchCount != 0)
		{
			Touch touch;
			if (!GetTrackedTouch(out touch))
			{
				return Input.mousePosition;
			}
			return touch.position;
		}
		return Input.mousePosition;
	}
}
